package JavaSEAdvance.Lanlords;

import java.util.*;

/**
 * @author Xiao
 * @date 2021/7/28
 */
public class LanlordsDemo02 {
    /**
     * 模拟斗地主升级版
     * 通过程序实现斗地主过程中的洗牌、发牌和看牌。要求：对牌进行排序。
     * ♠   ♥   ♦   ♣    King    Queen
     */
    @SuppressWarnings("all")
    public static void main(String[] args) {
        ArrayList<String> pokerList = new ArrayList<>();
        getPokerList(pokerList);    //纸牌
        pokerList.add("Queen");
        pokerList.add("King");
        ArrayList<Integer> indexList = new ArrayList<>();
        HashMap<Integer, String> poker = new HashMap<>();
        getPokerHashMap(pokerList, indexList, poker);   //获取牌盒，获取编号
        Collections.shuffle(indexList);     //打散集合顺序,随机排序
        TreeSet<Integer> index1 = new TreeSet<>();
        TreeSet<Integer> index2 = new TreeSet<>();
        TreeSet<Integer> index3 = new TreeSet<>();
        TreeSet<Integer> index4 = new TreeSet<>();
        getPlayerIndex(indexList, index1, index2, index3, index4);    //玩家及底牌的编号
        ArrayList<String> player1 = new ArrayList<>();
        ArrayList<String> player2 = new ArrayList<>();
        ArrayList<String> player3 = new ArrayList<>();
        ArrayList<String> playerCards = new ArrayList<>();
        getPlayerPoker(poker, index1, index2, index3, index4, player1, player2, player3, playerCards);
        System.out.println("阿良的牌：" + player1);
        System.out.println("平安的牌：" + player2);
        System.out.println("左右的牌：" + player3);
        System.out.println("底牌：" + playerCards);
    }

    private static void getPlayerPoker(HashMap<Integer, String> poker,
                                       TreeSet<Integer> index1, TreeSet<Integer> index2, TreeSet<Integer> index3, TreeSet<Integer> index4,
                                       ArrayList<String>...arr) {
        poker.forEach((Integer i, String s) -> {
            if (index1.contains(i)) {
                arr[0].add(s);
            }
            if (index2.contains(i)) {
                arr[1].add(s);
            }
            if (index3.contains(i)) {
                arr[2].add(s);
            }
            if (index4.contains(i)) {
                arr[3].add(s);
            }
        });
    }

    /**
     * 将牌盒中的编号分成三组并排序
     */
    private static void getPlayerIndex(ArrayList indexList, TreeSet...arr) {
        for (int i = 0; i < indexList.size()-5; i+=3) {
           arr[0].add(indexList.get(i));
            arr[1].add(indexList.get(i + 1));
            arr[2].add(indexList.get(i + 2));
        }
        for (int i = 0; i < 3; i++) {
            arr[3].add(indexList.get(indexList.size() - 1 - i));
        }
    }

    /**
     * 获取HashMap牌盒，牌盒中键为编号，值为牌
     */
    private static void getPokerHashMap(ArrayList<String> pokerList, ArrayList<Integer> indexList, HashMap<Integer, String> poker) {
        for (int i = 0; i < pokerList.size(); i++) {
            indexList.add(i);
            poker.put(indexList.get(i), pokerList.get(i));
        }
    }

    /**
     * 获取ArrayList牌盒，牌盒元素为牌
     */
    private static void getPokerList(ArrayList<String> pokerList) {
        String[] color = {"♠", "♥", "♦", "♣"};
        String[] number = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A","2"};
        for (String s : number) {
            for (String s1 : color) {
                pokerList.add(s1 + s);
            }
        }
    }
}
